<?php
/*
* 方法文件
* 
* @author      wangcleer
* @version     1.0
* @time        2018.09.08
*
*/ 


// 发送短信
function sendSms($phones,$msg){
    $url = "http://******/****/sendSMS.php?Phone=%s&msg=%s";        // 短信接口
    foreach ($phones as $key => $value) {
        $res = @file_put_contents(sprintf($url,$value,$msg));
        if ($res) {
            setLog('Send SMS to '.$value.' is successful');
        }else{
            setLog('Send SMS to '.$value.' is faild');
        }
    }
}


// 发送邮件
// $body 内容  $config配置信息
function sendMail($body,$config){

    // if php version >= 5.5 , use this class
    // require_once('mailer/PHPMailer.php');
    // require_once('mailer/SMTP.php');

    // php version 5.2
    require_once('mailer/class.phpmailer.php');
    require_once('mailer/class.smtp.php');

    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions

    try {
       
        $mail->SMTPDebug = false;                           // Enable verbose debug output
        $mail->isSMTP();                                    // Set mailer to use SMTP
        $mail->CharSet = "utf-8";                           // 定义邮件编码
        $mail->ContentType = "text/plain";                  // 文档类型                                   
        $mail->Host = $config['Host'];                      // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = $config['UserName'];                 // SMTP username
        $mail->Password = $config['Password'];                           // SMTP password
        $mail->SMTPSecure = $config['SmtpSecure'];                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = $config['Port'];                                    // TCP port to connect to

       
        $mail->setFrom($config['SetFrom']['from'], $config['SetFrom']['name']);

       
        foreach ($config['AddAddress'] as $key => $value) {
             $mail->addAddress($value); 
        }
      
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $config['Subject'];
        $mail->Body    = $body;
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        $mail->send();

        return 'Message has been sent';
       
    } catch (Exception $e) {

        return 'Message could not be sent. Mailer Error: '. $mail->ErrorInfo;

    }


}

// 日志记录 windos
function setLogUn($text){

	if(!is_dir('./log'))
	{
    	mkdir ('./log',0777,true);
    	file_put_contents('./log/log.txt','Create time '.date('Y-m-d H:i:s').PHP_EOL,FILE_APPEND);
	}
	$content = '##############'.PHP_EOL;
	$content .= date('Y-m-d H:i:s').' ----> '.$text.PHP_EOL; 
	file_put_contents('./log/log.txt',$content, FILE_APPEND);

}

// 日志记录 centos
function setLog($text){

    if(!is_dir('./log'))
    {
        mkdir ('./log',0777,true);
        file_put_contents('./log/log.txt','Create time '.date('Y-m-d H:i:s')."\r\n",FILE_APPEND);
    }
    $content = '##############'."\r\n";
    $content .= date('Y-m-d H:i:s').' ----> '.$text."\r\n"; 
    file_put_contents('./log/log.txt',$content, FILE_APPEND);

}

// 检查数据库配置

function inspectConfig($config){

    foreach ($config as $key => $value) {

        if($key == 'mailConfig'){
            foreach ($value as $k=>$v) {
                if(empty($v)){
                    die('请将邮箱信息配置完整！');
                }
            }
                
        }
        if($key == 'dbConfig'){
            foreach ($value as $v) {
                foreach ($v as $ite=>$item) {
                    if ($item == '') {
                        die('请将数据库信息配置完整！');
                    }
                }
            }
        }
       
    }

}





?>